'use strict'

const pkg = require('./pkg')
const { GRPC_CLIENT_ERROR_STATUSES, GRPC_SERVER_ERROR_STATUSES } = require('./constants')
const { getEnvironmentVariable: getEnv } = require('./config-helper')

// eslint-disable-next-line @stylistic/max-len
const qsRegex = String.raw`(?:p(?:ass)?w(?:or)?d|pass(?:_?phrase)?|secret|(?:api_?|private_?|public_?|access_?|secret_?)key(?:_?id)?|token|consumer_?(?:id|key|secret)|sign(?:ed|ature)?|auth(?:entication|orization)?)(?:(?:\s|%20)*(?:=|%3D)[^&]+|(?:"|%22)(?:\s|%20)*(?::|%3A)(?:\s|%20)*(?:"|%22)(?:%2[^2]|%[^2]|[^"%])+(?:"|%22))|bearer(?:\s|%20)+[a-z0-9\._\-]+|token(?::|%3A)[a-z0-9]{13}|gh[opsu]_[0-9a-zA-Z]{36}|ey[I-L](?:[\w=-]|%3D)+\.ey[I-L](?:[\w=-]|%3D)+(?:\.(?:[\w.+\/=-]|%3D|%2F|%2B)+)?|[\-]{5}BEGIN(?:[a-z\s]|%20)+PRIVATE(?:\s|%20)KEY[\-]{5}[^\-]+[\-]{5}END(?:[a-z\s]|%20)+PRIVATE(?:\s|%20)KEY|ssh-rsa(?:\s|%20)*(?:[a-z0-9\/\.+]|%2F|%5C|%2B){100,}`
// eslint-disable-next-line @stylistic/max-len
const defaultWafObfuscatorKeyRegex = String.raw`(?i)pass|pw(?:or)?d|secret|(?:api|private|public|access)[_-]?key|token|consumer[_-]?(?:id|key|secret)|sign(?:ed|ature)|bearer|authorization|jsessionid|phpsessid|asp\.net[_-]sessionid|sid|jwt`
// eslint-disable-next-line @stylistic/max-len
const defaultWafObfuscatorValueRegex = String.raw`(?i)(?:p(?:ass)?w(?:or)?d|pass(?:[_-]?phrase)?|secret(?:[_-]?key)?|(?:(?:api|private|public|access)[_-]?)key(?:[_-]?id)?|(?:(?:auth|access|id|refresh)[_-]?)?token|consumer[_-]?(?:id|key|secret)|sign(?:ed|ature)?|auth(?:entication|orization)?|jsessionid|phpsessid|asp\.net(?:[_-]|-)sessionid|sid|jwt)(?:\s*=([^;&]+)|"\s*:\s*("[^"]+"|\d+))|bearer\s+([a-z0-9\._\-]+)|token\s*:\s*([a-z0-9]{13})|gh[opsu]_([0-9a-zA-Z]{36})|ey[I-L][\w=-]+\.(ey[I-L][\w=-]+(?:\.[\w.+\/=-]+)?)|[\-]{5}BEGIN[a-z\s]+PRIVATE\sKEY[\-]{5}([^\-]+)[\-]{5}END[a-z\s]+PRIVATE\sKEY|ssh-rsa\s*([a-z0-9\/\.+]{100,})`

const service = getEnv('AWS_LAMBDA_FUNCTION_NAME') ||
  getEnv('FUNCTION_NAME') || // Google Cloud Function Name set by deprecated runtimes
  getEnv('K_SERVICE') || // Google Cloud Function Name set by newer runtimes
  getEnv('WEBSITE_SITE_NAME') || // set by Azure Functions
  pkg.name ||
  'node'

module.exports = {
  apiKey: undefined,
  appKey: undefined,
  apmTracingEnabled: true,
  'appsec.apiSecurity.enabled': true,
  'appsec.apiSecurity.sampleDelay': 30,
  'appsec.apiSecurity.endpointCollectionEnabled': true,
  'appsec.apiSecurity.endpointCollectionMessageLimit': 300,
  'appsec.blockedTemplateGraphql': undefined,
  'appsec.blockedTemplateHtml': undefined,
  'appsec.blockedTemplateJson': undefined,
  'appsec.enabled': undefined,
  'appsec.eventTracking.mode': 'identification',
  // TODO appsec.extendedHeadersCollection is deprecated, to delete in a major
  'appsec.extendedHeadersCollection.enabled': false,
  'appsec.extendedHeadersCollection.redaction': true,
  'appsec.extendedHeadersCollection.maxHeaders': 50,
  'appsec.obfuscatorKeyRegex': defaultWafObfuscatorKeyRegex,
  'appsec.obfuscatorValueRegex': defaultWafObfuscatorValueRegex,
  'appsec.rasp.enabled': true,
  // TODO Deprecated, to delete in a major
  'appsec.rasp.bodyCollection': false,
  'appsec.rateLimit': 100,
  'appsec.rules': undefined,
  'appsec.sca.enabled': null,
  'appsec.stackTrace.enabled': true,
  'appsec.stackTrace.maxDepth': 32,
  'appsec.stackTrace.maxStackTraces': 2,
  'appsec.wafTimeout': 5e3, // µs
  baggageMaxBytes: 8192,
  baggageMaxItems: 64,
  baggageTagKeys: 'user.id,session.id,account.id',
  clientIpEnabled: false,
  clientIpHeader: null,
  'cloudPayloadTagging.requestsEnabled': false,
  'cloudPayloadTagging.responsesEnabled': false,
  'cloudPayloadTagging.maxDepth': 10,
  'cloudPayloadTagging.rules': [],
  'crashtracking.enabled': true,
  'codeOriginForSpans.enabled': true,
  'codeOriginForSpans.experimental.exit_spans.enabled': false,
  dbmPropagationMode: 'disabled',
  'dogstatsd.hostname': '127.0.0.1',
  'dogstatsd.port': '8125',
  dsmEnabled: false,
  'dynamicInstrumentation.captureTimeoutMs': 15,
  'dynamicInstrumentation.enabled': false,
  'dynamicInstrumentation.probeFile': undefined,
  'dynamicInstrumentation.redactedIdentifiers': [],
  'dynamicInstrumentation.redactionExcludedIdentifiers': [],
  'dynamicInstrumentation.uploadIntervalSeconds': 1,
  env: undefined,
  'experimental.aiguard.enabled': false,
  'experimental.aiguard.endpoint': undefined,
  'experimental.aiguard.maxMessagesLength': 16,
  'experimental.aiguard.maxContentSize': 512 * 1024,
  'experimental.aiguard.timeout': 10_000, // ms
  'experimental.enableGetRumData': false,
  'experimental.exporter': undefined,
  'experimental.flaggingProvider.enabled': false,
  'experimental.flaggingProvider.initializationTimeoutMs': 30_000,
  flushInterval: 2000,
  flushMinSpans: 1000,
  gitMetadataEnabled: true,
  graphqlErrorExtensions: [],
  'grpc.client.error.statuses': GRPC_CLIENT_ERROR_STATUSES,
  'grpc.server.error.statuses': GRPC_SERVER_ERROR_STATUSES,
  headerTags: [],
  'heapSnapshot.count': 0,
  'heapSnapshot.destination': '',
  'heapSnapshot.interval': 3600,
  hostname: '127.0.0.1',
  'iast.dbRowsToTaint': 1,
  'iast.deduplicationEnabled': true,
  'iast.enabled': false,
  'iast.maxConcurrentRequests': 2,
  'iast.maxContextOperations': 2,
  'iast.redactionEnabled': true,
  'iast.redactionNamePattern': null,
  'iast.redactionValuePattern': null,
  'iast.requestSampling': 30,
  'iast.securityControlsConfiguration': null,
  'iast.telemetryVerbosity': 'INFORMATION',
  'iast.stackTrace.enabled': true,
  injectionEnabled: [],
  'installSignature.id': null,
  'installSignature.time': null,
  'installSignature.type': null,
  instrumentationSource: 'manual',
  injectForce: null,
  isAzureFunction: false,
  isCiVisibility: false,
  isEarlyFlakeDetectionEnabled: false,
  isFlakyTestRetriesEnabled: false,
  flakyTestRetriesCount: 5,
  isGCPFunction: false,
  isGitUploadEnabled: false,
  isIntelligentTestRunnerEnabled: false,
  isManualApiEnabled: false,
  'langchain.spanCharLimit': 128,
  'langchain.spanPromptCompletionSampleRate': 1,
  'llmobs.agentlessEnabled': undefined,
  'llmobs.enabled': false,
  'llmobs.mlApp': undefined,
  ciVisibilityTestSessionName: '',
  ciVisAgentlessLogSubmissionEnabled: false,
  legacyBaggageEnabled: true,
  isTestDynamicInstrumentationEnabled: false,
  isServiceUserProvided: false,
  testManagementAttemptToFixRetries: 20,
  isTestManagementEnabled: false,
  isImpactedTestsEnabled: false,
  logInjection: true,
  otelLogsEnabled: false,
  otelUrl: undefined,
  otelLogsUrl: undefined, // Will be computed using agent host
  otelHeaders: undefined,
  otelLogsHeaders: '',
  otelProtocol: 'http/protobuf',
  otelLogsProtocol: 'http/protobuf',
  otelLogsTimeout: 10_000,
  otelTimeout: 10_000,
  otelBatchTimeout: 5000,
  otelMaxExportBatchSize: 512,
  otelMaxQueueSize: 2048,
  otelMetricsEnabled: false,
  otelMetricsUrl: undefined, // Will be computed using agent host
  otelMetricsHeaders: '',
  otelMetricsProtocol: 'http/protobuf',
  otelMetricsTimeout: 10_000,
  otelMetricsExportTimeout: 7500,
  otelMetricsExportInterval: 10_000,
  otelMetricsTemporalityPreference: 'DELTA', // DELTA, CUMULATIVE, or LOWMEMORY
  lookup: undefined,
  inferredProxyServicesEnabled: false,
  memcachedCommandEnabled: false,
  middlewareTracingEnabled: true,
  openAiLogsEnabled: false,
  'openai.spanCharLimit': 128,
  peerServiceMapping: {},
  plugins: true,
  port: '8126',
  'profiling.enabled': undefined,
  'profiling.exporters': 'agent',
  'profiling.sourceMap': true,
  'profiling.longLivedThreshold': undefined,
  protocolVersion: '0.4',
  queryStringObfuscation: qsRegex,
  'remoteConfig.enabled': true,
  'remoteConfig.pollInterval': 5, // seconds
  reportHostname: false,
  resourceRenamingEnabled: false,
  'runtimeMetrics.enabled': false,
  'runtimeMetrics.eventLoop': true,
  'runtimeMetrics.gc': true,
  runtimeMetricsRuntimeId: false,
  sampleRate: undefined,
  'sampler.rateLimit': 100,
  'sampler.rules': [],
  'sampler.spanSamplingRules': [],
  scope: undefined,
  service,
  serviceMapping: {},
  site: 'datadoghq.com',
  spanAttributeSchema: 'v0',
  spanComputePeerService: false,
  spanLeakDebug: 0,
  spanRemoveIntegrationFromService: false,
  startupLogs: false,
  'stats.enabled': false,
  tags: {},
  tagsHeaderMaxLength: 512,
  'telemetry.debug': false,
  'telemetry.dependencyCollection': true,
  'telemetry.enabled': true,
  'telemetry.heartbeatInterval': 60_000,
  'telemetry.logCollection': true,
  'telemetry.metrics': true,
  traceEnabled: true,
  traceId128BitGenerationEnabled: true,
  traceId128BitLoggingEnabled: true,
  tracePropagationExtractFirst: false,
  tracePropagationBehaviorExtract: 'continue',
  'tracePropagationStyle.inject': ['datadog', 'tracecontext', 'baggage'],
  'tracePropagationStyle.extract': ['datadog', 'tracecontext', 'baggage'],
  'tracePropagationStyle.otelPropagators': false,
  traceWebsocketMessagesEnabled: false,
  traceWebsocketMessagesInheritSampling: true,
  traceWebsocketMessagesSeparateTraces: true,
  tracing: true,
  url: undefined,
  version: pkg.version,
  instrumentation_config_id: undefined,
  'vertexai.spanCharLimit': 128,
  'vertexai.spanPromptCompletionSampleRate': 1,
  'trace.aws.addSpanPointers': true,
  'trace.dynamoDb.tablePrimaryKeys': undefined,
  'trace.nativeSpanEvents': false
}
